<template>
	<view class="main-content">
		<MHeader>{{ state.pageName }}</MHeader>
		<scroll-view scroll-y="true" style="height: calc(100% - 180rpx)">
			<view>
				<item name="结算单号" must>
					<input class="input" v-model="state.info.Data.Model.FBillNo"  placeholder="请输入结算单号" />
				</item>
				<item name="数据状态"  >
					<input class="input" v-model="state.info.Data.Model.FDocumentStatus" disabled placeholder="请选择" />
				</item>
				<item name="组织"  must>
					<input class="input" v-model="state.info.Data.Model.F_QUQY_ORGID"  placeholder="请选择" />
				</item>
				<item name="身份证号" >
					<input class="input" v-model="state.info.Data.Model.F_QUQY_SFID"  placeholder="请输入结算单号" />
				</item>
				<item name="合同编号" >
					<input class="input" v-model="state.info.Data.Model.F_QUQY_HTNUMBER"  placeholder="请输入合同编号" />
				</item>
				<van-field v-model="state.info.Data.Model.F_QUQY_HTQTY" type="number" label="合同数量" input-align="right" placeholder="请输入"/>
				<!-- <item name="合同数量" >
					<input class="input" v-model="state.info.Data.Model.F_QUQY_HTQTY" type="number"  placeholder="请输入合同数量" />
				</item> -->
				<item name="上苗日期"  >
					<input class="input" v-model="state.info.Data.Model.F_QUQY_SEEDDATE"  @click="chooseT('F_QUQY_SEEDDATE')" placeholder="请选择上苗日期" />
				</item>
				<item name="回收日期"  >
					<input class="input" v-model="state.info.Data.Model.F_QUQY_HSDATE"  @click="chooseT('F_QUQY_HSDATE')" placeholder="请选择回收日期" />
				</item>
				
				<item name="总胴体"  >
					<input class="input" v-model="state.info.Data.Model.F_QUQY_CARCASS" placeholder="请输入总胴体" />
				</item>
				<item name="料肉比"  >
					<input class="input" v-model="state.info.Data.Model.F_QUQY_LRPERCENT" placeholder="请输入料肉比" />
				</item>
				<item name="料运费"  >
					<input class="input" v-model="state.info.Data.Model.F_QUQY_LFREIGHT" placeholder="请输入料运费" />
				</item>
				<item name="风险金"  >
					<input class="input" v-model="state.info.Data.Model.F_QUQY_FXPRICE" placeholder="请输入风险金" />
				</item>
				<item name="总收入"  >
					<input class="input" v-model="state.info.Data.Model.F_QUQY_ALLINAMOUNT" placeholder="请输入总收入" />
				</item>
				<item name="总补贴"  >
					<input class="input" v-model="state.info.Data.Model.F_QUQY_ALLSUBAMOUNT" placeholder="请输入总补贴" />
				</item>
				<item name="总支出"  >
					<input class="input" v-model="state.info.Data.Model.F_QUQY_ALLOUTAMOUNT" placeholder="请输入总支出" />
				</item>
				<item name="养殖利润"  >
					<input class="input" v-model="state.info.Data.Model.F_QUQY_PROFIT" placeholder="请输入养殖利润" />
				</item>
				<item name="已收款"  >
					<input class="input" v-model="state.info.Data.Model.F_QUQY_RECEIVEAMOUNT" placeholder="请输入已收款" />
				</item>
				<item name="实付金额"  >
					<input class="input" v-model="state.info.Data.Model.F_QUQY_PAYAMOUNT" placeholder="请输入实付金额" />
				</item>
				<item name="付款账号"  >
					<input class="input" v-model="state.info.Data.Model.F_QUQY_PAYACCOUNT" placeholder="请输入付款账号" />
				</item>
				<item name="结算状态"  >
					<input class="input" v-model="state.info.Data.Model.F_QUQY_JSSTATUS" placeholder="请输入结算状态" />
				</item>
		<!-- 		
				// ： 
				// ： 
				// 实付金额： 
				// ：F_QUQY_PAYACCOUNT 
				//  ：F_QUQY_JSSTATUS  (必填项) -->
				
			</view>
		</scroll-view>
		<view class="btns">
			<van-button type="primary" size="small" @click="save">保 存</van-button>
		</view>
	</view>
	<ChooseTime ref="chooseTime" />
	<Loading v-if="state.loading" />
</template>

<script setup>
	import moment from 'moment'
	import { reactive, getCurrentInstance, ref, watch, nextTick } from 'vue'
	import { onLoad } from '@dcloudio/uni-app'
	import { checkForm } from '/utils/utils.js'
	import kdApis from '/api/modularApi/kdApi.js'
	import { confirm, msg } from '/utils/utils.js'
	import { includes } from 'lodash-es'
	const { proxy } = getCurrentInstance()
	const state = reactive({
		loading: true,
		info: {
			FormId: 'QUQY_VeterinaryMedicineApplication',
			Data: {
				Model: {
						FBillNo:"",
						FDocumentStatus:"暂存", 
						F_QUQY_BREEDBATCH:"",  //(必填项)
						F_QUQY_ORGID:"", //(必填项)
						F_QUQY_SFID:"", 
						F_QUQY_HTNUMBER:"", 
						F_QUQY_HTQTY :"",
						F_QUQY_SEEDDATE:"", 
						F_QUQY_HSDATE:"",
						F_QUQY_CARCASS:"", 
						F_QUQY_LRPERCENT:"", 
						F_QUQY_LFREIGHT:"",
						F_QUQY_FXPRICE:"", 
						F_QUQY_ALLINAMOUNT:"", 
						F_QUQY_ALLSUBAMOUNT:"",
						F_QUQY_ALLOUTAMOUNT:"", 
						F_QUQY_PROFIT:"",
						F_QUQY_RECEIVEAMOUNT:"", 
						F_QUQY_PAYAMOUNT:"", 
						F_QUQY_PAYACCOUNT:"", 
						F_QUQY_JSSTATUS:"",  //(必填项)
					}
				
			}
		},
		rules: {
			FBillNo:"请输入结算单号",
			F_QUQY_BREEDBATCH: '请输入养殖批次！',
			F_QUQY_ORGID: '请选择组织！',
			F_QUQY_JSSTATUS: '请选择结算状态！',
		},
		pageName: ''
	});
	
	
	// 结算单号 // FBillNo
	//  数据状态： FDocumentStatus status: {
		// 	Z: '暂 存',
		// 	A: '保 存',
		// 	B: '审核中',
		// 	C: '已审核',
		// 	D: '重新审核'
		// }
	//   养殖批次：F_QUQY_BREEDBATCH  (必填项)
	//  组织：F_QUQY_ORGID  (必填项)
	//  身份证号：F_QUQY_SFID 
	//  合同编号：F_QUQY_HTNUMBER 
	//  合同数量：F_QUQY_HTQTY 
	//   上苗日期：F_QUQY_SEEDDATE 
	//  回收日期 F_QUQY_HSDATE
	//   总胴体：F_QUQY_CARCASS 
	//  料肉比：F_QUQY_LRPERCENT 
	// 料运费：F_QUQY_LFREIGHT
	// 风险金：F_QUQY_FXPRICE 
	//  总收入：F_QUQY_ALLINAMOUNT 
	// 总补贴：F_QUQY_ALLSUBAMOUNT
	// 总支出：F_QUQY_ALLOUTAMOUNT 
	// 养殖利润：F_QUQY_PROFIT 
	// 已收款：F_QUQY_RECEIVEAMOUNT 
	// 实付金额：F_QUQY_PAYAMOUNT 
	// 付款账号：F_QUQY_PAYACCOUNT 
	//  结算状态：F_QUQY_JSSTATUS  (必填项)
	
	

	const chooseTime = ref(null)
	const chooseT = (fieldName) => {
		chooseTime.value.start(state.info.Data.Model, fieldName);
	}
	
	const save = async () => {
		await checkForm(state.info.Data.Model, state.rules)
		// 🍕 至少一个子集 && 必填项
		if (state.info.Data.Model.F_QUQY_Entity.length == 0) return msg('请至少选择一种饲料！')
		let flag = true
		let flag2 = true
		state.info.Data.Model.F_QUQY_Entity.forEach((it, i) => {
			if (!it.F_QUQY_FeedName) {
				flag = false
				return msg(`请选择饲料（卡片${i+1}）！`)
			}
			if (!it.F_RSUS_ApplyQty) {
				flag2 = false
				return msg(`请输入申请数量（卡片${i+1}）！`)
			}
		})
		if (!flag || !flag2) return
		state.info.Data.Model.F_QUQY_ReturnOrNot = Number(state.info.Data.Model.F_QUQY_ReturnOrNot)
		kdApis.formSave(state.info).then(res => {
			if (res) {
				msg('保存成功！')
				setTimeout(() => {
					uni.navigateBack()
				}, 300)
			}
		})
	}

	// 1.组织控制（使用组织为当前选择的组织）：养殖批次
	const choose = (FormId, Field, FieldSon, FieldName, IdKey, IdKeyName, Group, BringOther, row) => {
		// 🐟 1
		let mustHasOrg = ['F_QUQY_FeedSupplier', 'F_QUQY_Feed']
		if (mustHasOrg.includes(Field)) {
			if (!state.info.Data.Model.F_QUQY_OrgIdName) return msg('请先选择养殖批次！')
		}
		
		// 选择饲料需要先选择饲料供应商
		if(Field == 'F_QUQY_Feed' && !state.info.Data.Model.F_QUQY_FeedSupplierName) {
			return msg('请先选择饲料供应商！')
		}
	
		// 选饲料供应商时，带入组织条件
		let FR = []
		if (mustHasOrg.includes(Field)) {
			FR = [{ Left: "(", FieldName: "FUseOrgId", Compare: "=", Value: state.curOrgId, Right: ")", Logic: "and" }]
		}
		
		if(Field == 'F_QUQY_Feed') {
			FR = [
				{ Left: "(", FieldName: "FUseOrgId", Compare: "=", Value: state.GYSOrgId, Right: ")", Logic: "and" },
				{ Left: "(", FieldName: "F_QUQY_IsMarket", Compare: "=", Value: state.F_QUQY_IsMarket, Right: ")", Logic: "and" }
				]
		}
		
		let url = `/pages/baseList/BaseList?FormId=${FormId}&isChoose=1&Group=${Group||''}&Name=请选择&R=1`
		if(FormId === 'BD_MATERIAL'){
			url = `/pages/baseList/BaseList?FormId=${FormId}&isChoose=1&Group=${Group||''}&Name=请选择&R=1&twice=1`
		}
		uni.navigateTo({
			url,
			success: res => { res.eventChannel.emit('fun', [fun, null, FR, state.materialList]) }
		})
	
		// 回调方法
		let fun = (list) => {
			console.log(list)
		}
	}

	onLoad(async (query) => {
		if (query.Id) {
			state.pageName = '养户结算单编辑'
			getDetails(query.Id)
		} else {
			state.pageName = '养户结算单新增'
			
			state.loading = false
		}
	})
</script>
<style lang="scss" scoped>
	.card {
		position: relative;
		width: calc(100% - 60rpx);
		margin: 20rpx auto;
		box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
		border-radius: 6rpx;

		.card-index {
			position: absolute;
			display: flex;
			// align-items: center;
			// justify-content: center;
			width: 40rpx;
			height: 40rpx;
			padding-left: 10rpx;
			font-size: 24rpx;
			background: #409eff;
			color: #fff;
			clip-path: polygon(0 0, 0 40rpx, 10rpx 40rpx, 40rpx 10rpx, 40rpx 0);
		}
	}
</style>
<style lang="scss" scoped>
	::v-deep .van-field {
		border-bottom: 1rpx solid #e9e9ea;
	}
	::v-deep .van-field__label {
		color: #000 !important;
	}
	::v-deep .van-field__control {
		text-align: right;
	}
	::v-deep .van-field__control::-webkit-input-placeholder {
	    color: gray;
	}
</style>